<?php defined('BASEPATH') OR exit('No direct script access allowed');


class Dept_model extends Base_model
{
	protected $_table_name = 'dept';
    protected $_key_name = 'id';
	
	public function __construct()
    {
        parent::__construct();
		
    }

    public function alls($map_arr = NULL)
    {
        return $this->_menus_info();
    }

    private function _menus_info($pid = 0,$level = 0,$parentName = NULL)
    {
        $this->select("id,name,parent_id as parentId,order_num as orderNum,ctime as createTime ");
        $this->where('dtime',0);
        $this->where('parent_id',$pid);
        $this->order_by("order_num","DESC");

        $data_arr = $this->all();

        if($data_arr)
        {
            foreach($data_arr as &$val)
            {
                $i = $level;
                $val->level = $level;
                $val->parentName = $parentName;
                $val->createTime = date("Y-m-d H:i:s",$val->createTime);
                $i = $level+1;
                $val->children = $this->_menus_info($val->id,$i,$val->name);
            }
        }

        return $data_arr;
    }

    public function saves($data_arr = NULL)
    {
        if($data_arr == NULL) return false;

        $data_arr["ctime"] = time();
        return $this->replace($data_arr);
    }

    public function dels($ids_arr = NULL)
    {
        if($ids_arr == NULL) return false;
        $this->where_in($this->_key_name,$ids_arr);
        $data_arr['dtime'] = time();
        return $this->update($data_arr);
    }
	
}
/* End of file base_model.php */
/* Location: ./system/application/models/base_model.php */
